package com.ce.webframework.sec.web;


import java.util.List;

import com.ce.component.authorization.external.service.IBizSystemAuthorizationService;
import com.ce.pms.authorization.filter.PmsAuthenticateFilter;
import com.ce.pms.base.web.PmsBaseAction;
import com.ce.pms.common.vo.OrganizationalStructureVO;
import com.ce.webframework.sec.model.SecShortcutDO;
import com.ce.webframework.sec.model.SecShortcutTempDO;
import com.ce.webframework.sec.service.ISecMenuitemService;
import com.ce.webframework.sec.vo.SecMenuitemVO;
import com.ce.webframework.sec.vo.SecUserinfoVO;


public class IndexAction extends PmsBaseAction{


	IBizSystemAuthorizationService comAuthAuthorizationService;

	public void setComAuthAuthorizationService(
	IBizSystemAuthorizationService comAuthAuthorizationService) {
    this.comAuthAuthorizationService = comAuthAuthorizationService;
    }

	public IBizSystemAuthorizationService getAuthService() {
		return comAuthAuthorizationService;
	}




	private ISecMenuitemService secMenuitemService;

	public void setSecMenuitemService(ISecMenuitemService secMenuitemService) {
		this.secMenuitemService = secMenuitemService;
	}

	public void workTableDatas()
	{
		initMenu();
	}



	/**
	 * 初始化菜单
	 *
	 */
	public void initMenu(){

		this.getRequest().getSession().setMaxInactiveInterval(3600);

		List<SecMenuitemVO> menuList = null;
		OrganizationalStructureVO userinfoVO =(OrganizationalStructureVO)this.getRequest().getSession().getAttribute(PmsAuthenticateFilter.LOGIN_USER_KEY);
		List<String> funs = getAuthService().getAllFunctionEntitiesByType(userinfoVO.getAccountId(), PmsAuthenticateFilter.USER_RIGHT_TYPE_MENU,null);
		menuList = secMenuitemService.getMenuItemList(funs, null);
		getRequest().setAttribute("menuList", menuList);

	}



//	当前列表数据构造一个xml的字符串并返回
	private String generateResponseXml(List<SecShortcutDO> cutList){
		 String returnValue = null;
	        StringBuffer buffer = new StringBuffer();

	        if (cutList != null && cutList.size() > 0) {
	        	buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
	        	buffer.append("<shortcut-list>");
	    		for (int i = 0; i < cutList.size(); i++) {
	    			SecShortcutTempDO node = cutList.get(i).getSecShortcutTemp();
	    			buffer.append("<shortcut>");
			        buffer.append("<shortcutMode>"+node.getShortcutName()+"</shortcutMode>");
			        buffer.append("<pageUrl>"+node.getPageUrl()+"</pageUrl>");
			        buffer.append("<menuIds>"+node.getMenuPath()+"</menuIds>");
			        buffer.append("<shortcutId>"+node.getShortcutTempId()+"</shortcutId>");
			        buffer.append("</shortcut>");

	    		}
	    		buffer.append("</shortcut-list>");
	        }
	        returnValue = buffer.toString();

	        return returnValue;

	}


	/**
	 * 查询当前用户所有的快捷方式
	 * @return List
	 */
	public void getAllCutModes(){
//		String type = this.getRequest().getParameter("cut_type");
//		List <String>functionIds=getAuthInfo().getMenuIDList();
//		List<SecShortcutTempDO> cutList =
//			secShortcutService.getAllCutModes(type,functionIds,getLoginUser().getEpId());
//		String  formatMsg = generateXml(cutList);
//		this.renderXML(formatMsg);
	}


	private String generateXml(List<SecShortcutTempDO> cutList){
		 String returnValue = null;
	        StringBuffer buffer = new StringBuffer();

	        if (cutList != null && cutList.size() > 0) {
	        	buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
	        	buffer.append("<shortcut-list>");
	    		for (int i = 0; i < cutList.size(); i++) {
	    			SecShortcutTempDO node = cutList.get(i);
	    			SecShortcutDO shortcut = node.getSecShortcut();
	    			System.out.println("shortcut obj========"+shortcut);
	    			buffer.append("<shortcut>");
			        buffer.append("<shortcutMode>"+node.getShortcutName()+"</shortcutMode>");
			        buffer.append("<shortcutId>"+node.getShortcutTempId()+"</shortcutId>");
			        buffer.append("<userId>"+(shortcut==null?"null":shortcut.getUserId())+"</userId>");
			        buffer.append("</shortcut>");

	    		}
	    		buffer.append("</shortcut-list>");
	        }
	        returnValue = buffer.toString();

	        return returnValue;

	}

	public void getRems(){
		String returnValue = null;
		StringBuffer buffer = new StringBuffer();

		OrganizationalStructureVO user = getLoginUser();
			buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
			buffer.append("<msg-list>");
//			List<SecScheduleDO>  list = secScheduleService.getDayRemiderList(user.getUserId(), new Date());
//			for (SecScheduleDO secScheduleDo : list) {
//    			buffer.append("<msg>");
//    			buffer.append("<id>"+ secScheduleDo.getScheduleId() +"</id>");
//    			buffer.append("<timeOut>"+ secScheduleDo.getTimeGap() +"</timeOut>");
//    			buffer.append("<title>"+ secScheduleDo.getTitle() +"</title>");
//		        buffer.append("<time>"+ secScheduleDo.getStartTimeFormat() +"</time>");
//		        buffer.append("<detail>"+ secScheduleDo.getContent() +"</detail>");
//		        buffer.append("<repeat>"+ secScheduleDo.getRepeatCycle() +"</repeat>");
//		        buffer.append("<remind>"+ secScheduleDo.getRemindCycle() +"</remind>");
//		        buffer.append("</msg>");
//			}
    	buffer.append("</msg-list>");

        returnValue = buffer.toString();

        this.renderXML(returnValue);

	}




}
